package com.base.serviceImpl;

import com.base.feign.SysyemClient;
import com.base.mapper.UserMapper;
import com.base.model.BizCode;
import com.base.model.ResponseBase;
import com.base.model.User;
import com.base.service.UserCenterService;
import com.base.util.DateUtil;
import com.base.util.JsonUtils;
import com.base.util.RedisUtil;
import com.base.util.UuidUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName: UserCenterServiceImpl
 * @Description: 用户中心接口实现类
 * @Author: CJQ
 * @Date: 2021/2/2 10:15
 */
@Service
@Slf4j
public class UserCenterServiceImpl implements UserCenterService {

    @Autowired
    private SysyemClient systemClient;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserMapper userMapper;

    @Override
    public String getToken(String account) {
        String token = systemClient.hello(account);
        redisUtil.setValue(account, token, 60);
        log.info("redis缓存用户" + account + "的token=" + redisUtil.getValue(account));
        log.info("用户中心模块:UserCenterServiceImpl获取用户TOKEN接口" + token);
        return token;
    }

    /**
     * 获取用户列表
     *
     * @param pageNum
     * @param account
     * @return
     */
    public PageInfo<User> getUsers(int pageNum, String account) {
        log.debug("UserService.getUsers参数:pageNum=" + pageNum + "&&account=" + account);
        PageInfo<User> list = PageHelper.startPage(pageNum, 10).doSelectPageInfo(() -> userMapper.selectUserList(account));
        return list;
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    public ResponseBase editUser(User user) {
        log.debug("UserService.editUser参数:user=" + JsonUtils.tojson(user));
        ResponseBase responseBase = new ResponseBase();
        try {
            userMapper.updateByPrimaryKeySelective(user);
            responseBase.setMessage(BizCode.BizCode_000000.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_000000.getCode());
            responseBase.setStatus(true);
        } catch (Exception e) {
            log.error("UserService.editUser方法异常:" + e.getMessage());
            e.printStackTrace();
            responseBase.setMessage(e.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_999999.getCode());
            responseBase.setStatus(false);
        }
        responseBase.setDateTime(DateUtil.getFormatTimeNow("yyyy-MM-dd HH:mm:ss"));
        return responseBase;
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    public ResponseBase deleteUser(String userId) {
        log.debug("UserService.deleteUser参数:userId=" + userId);
        ResponseBase responseBase = new ResponseBase();
        try {
            User user = new User();
            user.setId(userId);
            user.setStatus("0");
            userMapper.updateByPrimaryKeySelective(user);
            responseBase.setMessage(BizCode.BizCode_000000.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_000000.getCode());
            responseBase.setStatus(true);
        } catch (Exception e) {
            log.error("UserService.deleteUser方法异常:" + e.getMessage());
            e.printStackTrace();
            responseBase.setMessage(e.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_999999.getCode());
            responseBase.setStatus(false);
        }
        responseBase.setDateTime(DateUtil.getFormatTimeNow("yyyy-MM-dd HH:mm:ss"));
        return responseBase;
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    public ResponseBase addUser(User user) {
        log.debug("UserService.addUser参数:user=" + JsonUtils.tojson(user));
        ResponseBase responseBase = new ResponseBase();
        try {
            user.setId(UuidUtil.getUUID());
            user.setStatus("1");
            user.setCreatedate(DateUtil.getFormatTimeNow("yyyy-MM-dd HH:mm:ss"));
            userMapper.insertSelective(user);
            responseBase.setMessage(BizCode.BizCode_000000.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_000000.getCode());
            responseBase.setStatus(true);
        } catch (Exception e) {
            log.error("UserService.addUser方法异常:" + e.getMessage());
            e.printStackTrace();
            responseBase.setMessage(e.getMessage());
            responseBase.setReturnCode(BizCode.BizCode_999999.getCode());
            responseBase.setStatus(false);
        }
        responseBase.setDateTime(DateUtil.getFormatTimeNow("yyyy-MM-dd HH:mm:ss"));
        return responseBase;
    }
}
